package thread;

import java.util.concurrent.atomic.AtomicInteger;

public class Demo45 {
    private static final AtomicInteger counter = new AtomicInteger(0);

    static  class IncrementTask implements Runnable{
        @Override
        public void run() {
            for (int i = 0; i < 1000; i++) {
                counter.incrementAndGet();//原子性自增
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Thread[] threads = new Thread[10];

        for (int i = 0; i < threads.length; i++) {
            threads[i] = new Thread(new IncrementTask());
            threads[i].start();
        }

        for (Thread thread:threads) {
            thread.join();
        }

        System.out.println(counter.get());
    }
}
